<template>
	<view class="public-vote">
		<image class="findback" :style="'height:200rpx'" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/find/findback.png" mode="aspectFit"></image>
		<headers title="发布投票" position="fixed" showback="true" navBackcolor="transparent">
		</headers>
		<view class="public-vote-main">
			<view class="public-vote-main-input">
				<textarea v-model="text" placeholder="添加正文" maxlength="200" />
				<view class="public-vote-main-input-max">
					{{text.length}}/200
				</view>
			</view>
			<view class="public-vote-main-tools">
				<view class="public-vote-main-tools-item u-flex" @click="goPosition()">
					<view class="public-vote-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/address.png" mode=""></image>
						<view class="public-vote-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								所在位置
							</view>
						</view>
					</view>
					<view class="public-vote-main-tools-item-right u-flex">
						<view class="public-vote-main-tools-item-right-content">
							{{ address }}
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
				<view class="public-vote-main-tools-item u-flex">
					<view class="public-vote-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/option.png" mode=""></image>
						<view class="public-vote-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								选项规则
							</view>
						</view>
					</view>
					<view class="public-vote-main-tools-item-right u-flex">
						<view class="public-vote-main-tools-item-right-content">
							单选
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
				<view class="public-vote-main-tools-options">
					<view class="public-vote-main-tools-options-item u-flex" v-for="(item, index) in optionList" :key="index">
						<view class="public-vote-main-tools-options-item-left">
							{{ index + 1 }}
						</view>
						<view class="public-vote-main-tools-options-item-main">
							<input type="text" v-model="optionList[index]"
								:placeholder="'选项' + (index + 1 == 1 ? '一' : index + 1 == 2 ? '二' : index + 1 == 3 ? '三' : index + 1 == 4 ? '四' : index + 1 == 5 ? '五' : index + 1 == 6 ? '六' : index + 1 == 7 ? '七' : index + 1 == 8 ? '八' : index + 1 == 9 ? '九' : '十')"
								placeholder-style="color: #8A9199;">
						</view>
						<view class="public-vote-main-tools-options-item-right u-flex"
							v-if="(index + 1) == optionList.length">
							<view class="public-vote-main-tools-options-item-right-img" @click="addOptions()">
								<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/add.png" mode=""></image>
							</view>
							<view class="public-vote-main-tools-options-item-right-img" @click="delOption(index)">
								<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/del.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="public-vote-main-btn">
				<!-- <view class="public-btn public-btn-color">
					发布
				</view> -->
				<view class="btns">
					<image class="btns-img" v-if="allowPost" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/btn/576x88.png" mode=""></image>
					<view class="btns-con" :style="allowPost ? 'color: #FFFFFF;' : 'color: #8A9199;'" @click="postFun()">
						发布
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import headers from '@/components/headers/index.vue'
export default {
	// 组件名称
	name: 'demo',
	// 组件参数 接收来自父组件的数据
	props: {},
	// 局部注册的组件,import引入的组件需要注入到对象中才能使用
	components: {
		headers
	},
	// 组件状态值
	data() {
		return {
			text: "",
			address: '',
			positionInfo: {},
			allowPost: false,

			optionList: [""],
			post: {
				type: "3",
				content: "",
				address: "",
				longitude: "",
				latitude: "",
				option: "",
				province: "",
				city: "",
				area: "",
			}
		}
	},
	// 计算属性
	computed: {},
	// 侦听器
	watch: {
		'post': {
			handler() {
				this.allowPost = this.canPost()
				console.log(this.allowPost)
			},
			deep: true
		},
		'text': {
			handler() {
				this.allowPost = this.canPost()
				console.log(this.allowPost)
			}
		}
	},
	// 组件方法
	methods: {
		// 验证是否填写完整
		canPost() {
			if (this.text.length == 0) {
				return '请输入正文'
			}
			if (this.post.latitude == '' || this.post.longitude == '') {
				return '请选择地址'
			}
			if (this.optionList.length == 0 || this.optionList[0] == '') {
				return '请输入选项'
			}
			return true
		},
		addOptions() {
			this.optionList.push("")
		},
		delOption(index) {
			if (this.optionList.length != 1) {
				this.optionList.splice(index - 1, 1)
			} else {

			}
		},
		getLocation() {
			this.post.address = this.positionInfo.address
			this.post.longitude = this.positionInfo.location.lng
			this.post.latitude = this.positionInfo.location.lat
			this.post.province = this.positionInfo.ad_info.province
			this.post.city = this.positionInfo.ad_info.city
			this.post.area = this.positionInfo.ad_info.district
		},
		// 跳转到选择位置页面
		goPosition() {
			this.$Router.push({
				path: '/pages/public/components/SelectedPosition'
			})
		},
		// 发布帖子
		postFun() {
			let that = this;
			if (that.canPost() == true) {
				let item = {}
				item = JSON.parse(JSON.stringify(that.post))
				item.content = that.text
				item.option = that.optionList.toString()
				console.log(item,this.optionList)
				that.$http('public.pushTrends', item, '').then(res => {
					uni.showToast({
						title: res.info,
						icon: 'none',
						position: 'top'
					});
					this.$Router.back()
				});
			} else {
				uni.showToast({
					title: that.canPost(),
					icon: 'none',
					position: 'top'
				});
			}
		},
	},
	// 以下是生命周期钩子   注：没用到的钩子请自行删除
	/**
	 * 在实例初始化之后，组件属性计算之前，如data属性等
	 */
	beforeCreate() { },
	/**
	 * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
	 */
	created() { },
	/**
	 * 在挂载开始之前被调用：相关的 render 函数首次被调用。
	 */
	beforeMount() { },
	/**
	 * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
	 * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
	 */
	mounted() { },
	/**
	 * 数据更新时调用，发生在虚拟 DOM 重新渲染和打补丁之前。
	 * 你可以在这个钩子中进一步地更改状态，这不会触发附加的重渲染过程。
	 */
	beforeUpdate() { },
	/**
	 * 由于数据更改导致的虚拟 DOM 重新渲染和打补丁，在这之后会调用该钩子。
	 * 当这个钩子被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。
	 */
	updated() { },
	/**
	 * keep-alive 组件激活时调用。 仅针对keep-alive 组件有效
	 */
	activated() { },
	/**
	 * keep-alive 组件停用时调用。 仅针对keep-alive 组件有效
	 */
	deactivated() { },
	/**
	 * 实例销毁之前调用。在这一步，实例仍然完全可用。
	 */
	beforeDestroy() { },
	/**
	 * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
	 * 所有的事件监听器会被移除，所有的子实例也会被销毁。
	 */
	destroyed() { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<style scoped lang="scss">
.findback {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	z-index: 6;
	// background: #FFFFFF;
}

.public-vote {
	width: 100%;
	height: 100%;
	// background: linear-gradient(#F1EFFFFF 0%, #FFFFFF 20%);
	// padding-top: $nav-top-height;

	&-main {
		padding: 130rpx 32rpx 0;

		&-input {
			width: 100%;
			margin: 32rpx 0 60rpx;
			padding-bottom: 32rpx;
			border-bottom: solid 1rpx #F5F5F5;

			&-max {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #8A9199;
				text-align: right;
			}

			textarea {
				width: 100%;
				height: 180rpx;
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				color: #2E3033;
				line-height: 44rpx;
			}
		}

		&-tools {
			&-item {
				justify-content: space-between;
				height: 100rpx;
				border-bottom: solid 1rpx #F5F5F5FF;

				&-left {
					image {
						margin-right: 10rpx;
						width: 32rpx;
						height: 32rpx;
					}

					&-content {
						position: relative;
					}

					&-content:after {
						content: '';
						display: block;
						width: 100%;

						position: absolute;
						left: 0;
						bottom: 0;
						height: 8rpx;
						border-radius: 6rpx;
						// background: #e7e5ffFF;
						background: rgba(164, 160, 254, .3);
						z-index: 0;
					}
				}

				&-right {
					image {
						width: 40rpx;
						height: 40rpx;
						margin-left: 10rpx;
					}

					&-content {
						font-size: 28rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						color: #8A9199;
					}

					.cash {
						.public-vote-main-tools-item-right-content-span {
							width: 98rpx;
							height: 56rpx;
							line-height: 56rpx;
							text-align: center;
							background: #F4F4F4;
							border-radius: 30rpx;
							font-size: 28rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #8A9199;
							margin-right: 16rpx;
						}

						.act {
							background: #7266FF;
							font-size: 28rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #FCFEFF;
						}
					}
				}
			}

			&-options {
				&-item {
					height: 100rpx;
					border-bottom: solid 1rpx #F5F5F5FF;

					&-left {
						background: #CCCCCC;
						border-radius: 50%;
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						text-align: center;
						color: #fff;
						margin-right: 16rpx;
						font-size: 24rpx;
					}

					&-main {
						flex: 1;

						input {
							font-size: 28rpx;
							font-weight: 400;
						}
					}

					&-right {
						width: 240rpx;
						display: flex;
						justify-content: flex-end;

						&-img {
							margin-right: 16rpx;

							image {
								width: 40rpx;
								height: 40rpx;
							}
						}

					}
				}
			}
		}

		&-btn {
			// .public-btn {
			// 	margin: 0 auto;
			// 	margin-top: 60rpx;
			// }
			padding: 104rpx 0;

			.btns {
				width: 576rpx;
				height: 88rpx;
				position: relative;
				background: #F5F5F5;
				border-radius: 32rpx;
				margin: 0 auto 0;

				&-img {
					width: 100%;
					height: 100%;
				}

				&-con {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #FAFCFF;
					line-height: 88rpx;
					text-align: center;
				}
			}
		}
	}
}
</style>
